package com.yun.controller;

import com.yun.pojo.User;
import com.yun.result.Result;
import com.yun.service.FileService;
import com.yun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	@Autowired
	private FileService fileService;

	@RequestMapping("/findUsername")
	public @ResponseBody
	Result<String> findUsername(HttpServletRequest request, String data) throws Exception {
		System.out.println(data);

		User user = userService.findUsername(data);
		System.out.println(user);
		if (user == null) {
			return new Result<String>(305, true, "可以注册");
		} else {
			return new Result<String>(305, false, "用户名已存在，请修改一个");
		}
	}

	/**
	 * 登录
	 */
	@RequestMapping("/goLogin")//解决提交空表单
	public String goLogin() {
		return "login";
	}

	@RequestMapping("/login")
	public String login(HttpServletRequest request, User user) {
		User exsitUser = userService.findUser(user);
		if (exsitUser != null) {
			HttpSession session = request.getSession();
			session.setAttribute(User.NAMESPACE, exsitUser.getUsername());
			session.setAttribute("totalSize", exsitUser.getTotalSize());
			return "redirect:/index.action";
		} else {
			request.setAttribute("msg", "用户名或密码错误");
			return "login";
		}
	}

	@RequestMapping("/githubLogin")
	public String githubLogin(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("githubLoginUsername");
		String password = (String) request.getSession().getAttribute("githubLoginPassword");
		System.out.println(username + "--" + password);
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		User user1 = userService.findUser(user);
		System.out.println("login:" + user);
		if (user1 != null) {
			HttpSession session = request.getSession();
			session.setAttribute(User.NAMESPACE, user1.getUsername());
			session.setAttribute("totalSize", user1.getTotalSize());
			return "redirect:/index.action";
		} else {
			request.setAttribute("msg", "用户名或密码错误");
			return "login";
		}
	}


	/**
	 * 注册
	 */
	@RequestMapping("/goRegister")//解决提交空表单
	public String goRegister() {

		return "register";

	}

	@RequestMapping("/register")
	public String register(HttpServletRequest request, User user) throws Exception {
		if (user.getUsername() == null ||
				user.getPassword() == null ||
				user.getUsername().equals("") ||
				user.getPassword().equals("")) //为空串是也能注册的问题
		{
			request.setAttribute("error", "用户名与密码不能为空");
			return "register";
		} else if (userService.findUsername(user.getUsername()) != null) {
			request.setAttribute("error", "用户名已存在");
			return "register";
		} else {
			boolean isSuccess = userService.addUser(user);
			if (isSuccess) {
				fileService.addNewNameSpace(request, user.getUsername());
				return "login";
			} else {
				request.setAttribute("error", "注册失败");
				return "register";
			}
		}
	}

	@RequestMapping("/githubRegister")
	public String githubRegister(HttpServletRequest request) throws Exception {
		User user = (User) request.getSession().getAttribute("githubUser");
		String username = user.getUsername();
		String password = user.getPassword();
		request.getSession().setAttribute("githubLoginUsername", username);
		request.getSession().setAttribute("githubLoginPassword", password);
		System.out.println("register:" + user);
		if (userService.findUsername(username) != null) {
			return "redirect:/user/githubLogin.action";
		}
		boolean isSuccess = userService.addUser(user);
		if (isSuccess) {
			fileService.addNewNameSpace(request, username);
			System.out.println("register:" + user);
			System.out.println(username + "--" + password);
			return "redirect:/user/githubLogin.action";
		} else {
			request.setAttribute("error", "注册失败");
			return "register";
		}
	}

	/**
	 * 登出
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
		return "redirect:/user/login.action";
	}


}
